﻿using CPPEI.Coolzon.Common;
using CPPEI.Coolzon.UI.Views;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using DT = CPPEI.Coolzon.Designtime.Metadata;
using UICore = CPPEI.Coolzon.UI;

namespace CPPEI.Coolzon.Designer.Views
{
    public partial class DesignView
    {
        private CPPEI.Coolzon.UI.Part scene = null;
        private void BeginLayoutPart()
        {
            _ctlCanvas.Cursor = Cursors.Cross;
            _designData.UpdateState(DesignState.DrawPart);
        }

        private void BeginResizePart()
        {
            _designData.UpdateState(DesignState.ResizePart);
        }

        /// <summary>
        /// 将设计器界面上的点转换为组件里的点
        /// </summary>
        /// <param name="ptInRoot"></param>
        /// <param name="rootElement"></param>
        /// <param name="partContainer"></param>
        /// <returns></returns>
        private Point DesignPointToPartPoint(Point ptInRoot, UIElement rootElement, Coolzon.UI.IPartContainer partContainer)
        {
            Point ptInPart = rootElement.TranslatePoint(ptInRoot, partContainer.GetContentContainer());
            return ptInPart;
        }

        private Coolzon.UI.Part _AddPartAndUpdateMetadata(Coolzon.UI.Part newPart, UICore.IPartContainer containerPart, Point position, Size size, string type, DT.NodeBase DT_MetadataForRemoved = null)
        {
            _layoutPartContainer = containerPart;

            if (_layoutPartContainer is Coolzon.UI.ScenePart)
            {
                #region 在场景中添加区域
                if (CurrentDesignMetadata4Scene.IsAllowSwapArea)
                {
                    // 场景的区域允许互换
                    // 场景的第一级区域，top，left设置无效，直接一次排列
                    Coolzon.UI.ScenePart scenePart = _layoutPartContainer as Coolzon.UI.ScenePart;
                    Logger.DebugToTag(Library.LogTAG, string.Format("在场景:{0}中添加了根级区域,场景允许移动区域", scenePart.Name));

                    double offsetX = 0;
                    List<Coolzon.UI.Part> subParts = scenePart.GetPartList();
                    for (int index = 0; index < subParts.Count; index++)
                    {
                        offsetX += subParts[index].XWidth + subParts[index].XMargin.Left + subParts[index].XMargin.Right;
                    }
                    newPart.XLeft = offsetX;
                    newPart.XTop = 0;
                    newPart.XWidth = size.Width;
                    newPart.XHeight = scenePart.Height;

                    #region 更新原数据
                    if(DT_MetadataForRemoved == null)
                    {
                        Guid areaId = Guid.NewGuid();
                        CPPEI.Coolzon.Designtime.Metadata.Area dtArea = new Designtime.Metadata.Area(areaId, newPart.Name, newPart.Name, true);
                        this.CurrentDesignMetadata4Scene.AddArea(dtArea);
                        CPPEI.Coolzon.UI.XProperty.XPropertyRTUtil.SetPropertyValue<double>(dtArea.RT_Area, "Width", newPart.XWidth);
                        CPPEI.Coolzon.UI.XProperty.XPropertyRTUtil.SetPropertyValue<double>(dtArea.RT_Area, "Height", newPart.XHeight);

                        // 设置元数据
                        newPart.SetMetadata(dtArea.RT_Area);
                    }
                    else
                    {
                        this.CurrentDesignMetadata4Scene.AddArea(DT_MetadataForRemoved as DT.Area);
                    }
                    #endregion
                }
                else
                {
                    Coolzon.UI.ScenePart scenePart = _layoutPartContainer as Coolzon.UI.ScenePart;
                    Logger.DebugToTag(Library.LogTAG, string.Format("在场景:{0}中添加了根级区域,场景不允许移动区域", scenePart.Name));
                    newPart.XLeft = position.X;
                    newPart.XTop = position.Y;
                    newPart.XWidth = size.Width;
                    newPart.XHeight = size.Height;

                    #region 更新原数据
                    if (DT_MetadataForRemoved == null)
                    {
                        Guid areaId = Guid.NewGuid();
                        CPPEI.Coolzon.Designtime.Metadata.Area dtArea = new Designtime.Metadata.Area(areaId, newPart.Name, newPart.Name, true);
                        this.CurrentDesignMetadata4Scene.AddArea(dtArea);

                        CPPEI.Coolzon.UI.XProperty.XPropertyRTUtil.SetPropertyValue<double>(dtArea.RT_Area, "Top", newPart.XTop);
                        CPPEI.Coolzon.UI.XProperty.XPropertyRTUtil.SetPropertyValue<double>(dtArea.RT_Area, "Left", newPart.XLeft);
                        CPPEI.Coolzon.UI.XProperty.XPropertyRTUtil.SetPropertyValue<double>(dtArea.RT_Area, "Width", newPart.XWidth);
                        CPPEI.Coolzon.UI.XProperty.XPropertyRTUtil.SetPropertyValue<double>(dtArea.RT_Area, "Height", newPart.XHeight);

                        // 设置元数据
                        newPart.SetMetadata(dtArea.RT_Area);
                    }
                    else
                    {
                        this.CurrentDesignMetadata4Scene.AddArea(DT_MetadataForRemoved as DT.Area);
                    }
                    #endregion
                }
                #endregion
            }
            else if (_layoutPartContainer is CPPEI.Coolzon.UI.AreaPart)
            {
                newPart.XLeft = position.X;
                newPart.XTop = position.Y;
                newPart.XWidth = size.Width;
                newPart.XHeight = size.Height;

                #region 在区域中添加了组件
                //从当前场景中获取设计时区域对象
                DT.Area dtContainerArea = DT.SolutionHelper.GetArea(this.Solution,
                    this.CurrentDesignMetadata4Scene.Name,
                    (_layoutPartContainer as UICore.Part).Name);
                #endregion

                #region 根据新组件的类型生成新的DTMetadata对象
                if (newPart is CPPEI.Coolzon.UI.AreaPart)
                {
                    if (DT_MetadataForRemoved == null)
                    {
                        Guid areaId = Guid.NewGuid();
                        CPPEI.Coolzon.Designtime.Metadata.Area dtArea = new Designtime.Metadata.Area(areaId, newPart.Name, newPart.Name, false);
                        dtContainerArea.AddArea(dtArea);

                        CPPEI.Coolzon.UI.XProperty.XPropertyRTUtil.SetPropertyValue<double>(dtArea.RT_Area, "Top", newPart.XTop);
                        CPPEI.Coolzon.UI.XProperty.XPropertyRTUtil.SetPropertyValue<double>(dtArea.RT_Area, "Left", newPart.XLeft);
                        CPPEI.Coolzon.UI.XProperty.XPropertyRTUtil.SetPropertyValue<double>(dtArea.RT_Area, "Width", newPart.XWidth);
                        CPPEI.Coolzon.UI.XProperty.XPropertyRTUtil.SetPropertyValue<double>(dtArea.RT_Area, "Height", newPart.XHeight);

                        // 设置元数据
                        newPart.SetMetadata(dtArea.RT_Area);
                    }
                    else
                    {
                        dtContainerArea.AddArea(DT_MetadataForRemoved as DT.Area);
                    }
                }
                else if (newPart is CPPEI.Coolzon.UI.ControlPart)
                {
                    if (DT_MetadataForRemoved == null)
                    {
                        Guid areaId = Guid.NewGuid();
                        CPPEI.Coolzon.Designtime.Metadata.Part dtPart = new Designtime.Metadata.Part(areaId, type, newPart.Name, newPart.Name);
                        dtContainerArea.AddPart(dtPart);

                        CPPEI.Coolzon.UI.XProperty.XPropertyRTUtil.SetPropertyValue<double>(dtPart.RT_Part, "Top", newPart.XTop);
                        CPPEI.Coolzon.UI.XProperty.XPropertyRTUtil.SetPropertyValue<double>(dtPart.RT_Part, "Left", newPart.XLeft);
                        CPPEI.Coolzon.UI.XProperty.XPropertyRTUtil.SetPropertyValue<double>(dtPart.RT_Part, "Width", newPart.XWidth);
                        CPPEI.Coolzon.UI.XProperty.XPropertyRTUtil.SetPropertyValue<double>(dtPart.RT_Part, "Height", newPart.XHeight);

                        // 设置元数据
                        newPart.SetMetadata(dtPart.RT_Part);
                    }
                    else
                    {
                        dtContainerArea.AddPart(DT_MetadataForRemoved as DT.Part);
                    }
                }
                #endregion
            }
            else if (_layoutPartContainer is CPPEI.Coolzon.UI.ControlPart)
            {
                #region 在组件中添加了组件
                DT.Part dtContainerPart = DT.SolutionHelper.GetPart(this.Solution,
                    this.CurrentDesignMetadata4Scene.Name,
                    (_layoutPartContainer as UICore.Part).Name);
                newPart.XLeft = position.X;
                newPart.XTop = position.Y;
                newPart.XWidth = size.Width;
                newPart.XHeight = size.Height;
                #endregion

                #region 根据新组件的类型生成新的DTMetadata对象
                if (newPart is CPPEI.Coolzon.UI.ControlPart)
                {
                    if (DT_MetadataForRemoved == null)
                    {
                        Guid areaId = Guid.NewGuid();
                        CPPEI.Coolzon.Designtime.Metadata.Part dtPart = new Designtime.Metadata.Part(areaId, type, newPart.Name, newPart.Name);
                        dtContainerPart.AddPart(dtPart);

                        CPPEI.Coolzon.UI.XProperty.XPropertyRTUtil.SetPropertyValue<double>(dtPart.RT_Part, "Top", newPart.XTop);
                        CPPEI.Coolzon.UI.XProperty.XPropertyRTUtil.SetPropertyValue<double>(dtPart.RT_Part, "Left", newPart.XLeft);
                        CPPEI.Coolzon.UI.XProperty.XPropertyRTUtil.SetPropertyValue<double>(dtPart.RT_Part, "Width", newPart.XWidth);
                        CPPEI.Coolzon.UI.XProperty.XPropertyRTUtil.SetPropertyValue<double>(dtPart.RT_Part, "Height", newPart.XHeight);

                        // 设置元数据
                        newPart.SetMetadata(dtPart.RT_Part);
                    }
                    else
                    {
                        dtContainerPart.AddPart(DT_MetadataForRemoved as DT.Part);
                    }
                }
                #endregion
            }
            _layoutPartContainer.AddPart(newPart);
            newPart.Init((_layoutPartContainer as Coolzon.UI.Part).PartContext);
            newPart.Active((_layoutPartContainer as Coolzon.UI.Part).PartContext);

            return newPart;
        }

        /// <summary>
        /// 设置组件大小
        /// 0, 1, 2,
        /// 3, 4, 5,
        /// 6, 7, 8,
        /// </summary>
        /// <param name="resizeDirection"></param>
        /// <param name="deltaX">deltaX</param>
        /// <param name="deltaY">deltaY</param>
        private void ResizePart(int resizeDirection, double deltaX, double deltaY)
        {
            double left = this._dbPosStore.X;
            double top = this._dbPosStore.Y;
            double width = this._dbSizeStore.Width;
            double height = this._dbSizeStore.Height;
            if (resizeDirection == 0)
            {
                #region // 左上
                width = this._dbSizeStore.Width - deltaX;
                if (width < 0)
                {
                    width = 0;
                }
                height = this._dbSizeStore.Height - deltaY;
                if (height < 0)
                {
                    height = 0;
                }

                left = this._dbPosStore.X + deltaX;
                top = this._dbPosStore.Y + deltaY;
                #endregion
            }
            else if (resizeDirection == 1)
            {
                #region // 上
                height = this._dbSizeStore.Height - deltaY;
                if (height < 0)
                {
                    height = 0;
                }

                top = this._dbPosStore.Y + deltaY;
                #endregion
            }
            else if (resizeDirection == 2)
            {
                #region 右上
                height = this._dbSizeStore.Height - deltaY;
                if (height < 0)
                {
                    height = 0;
                }
                width = this._dbSizeStore.Width + deltaX;
                if (width < 0)
                {
                    width = 0;
                }

                top = this._dbPosStore.Y + deltaY;
                #endregion
            }
            else if (resizeDirection == 3)
            {
                #region // 左
                width = this._dbSizeStore.Width - deltaX;
                if (width < 0)
                {
                    width = 0;
                }

                left = this._dbPosStore.X + deltaX;
                #endregion
            }
            else if (resizeDirection == 4)
            {
                // 移动组件的位置
                left = this._dbPosStore.X + deltaX;
                top = this._dbPosStore.Y + deltaY;
            }
            else if (resizeDirection == 5)
            {
                #region 右
                width = this._dbSizeStore.Width + deltaX;
                if (width < 0)
                {
                    width = 0;
                }
                #endregion
            }
            else if (resizeDirection == 6)
            {
                #region 左下
                width = this._dbSizeStore.Width - deltaX;
                if (width < 0)
                {
                    width = 0;
                }

                height = this._dbSizeStore.Height + deltaY;
                if (height < 0)
                {
                    height = 0;
                }

                left = this._dbPosStore.X + deltaX;
                #endregion
            }
            else if (resizeDirection == 7)
            {
                // 下
                height = this._dbSizeStore.Height + deltaY;
                if (height < 0)
                {
                    height = 0;
                }
            }
            else if (resizeDirection == 8)
            {
                // 右下
                width = this._dbSizeStore.Width + deltaX;
                if (width < 0)
                {
                    width = 0;
                }

                height = this._dbSizeStore.Height + deltaY;
                if (height < 0)
                {
                    height = 0;
                }
            }

            this._ctlDesignBorder.SetSize(new Size(width, height));
            this._ctlDesignBorder.SetOffset(new Point(left, top));
        }
        
        private void _OpenScene(string sceneName, bool isEditable)
        {
            _selectedPart = null;
            // 隐藏设计边框
            _ctlDesignBorder.Visibility = System.Windows.Visibility.Hidden;
            // 设置可编辑标志
            this._isEditable = isEditable;

            Coolzon.Metadata.Scene bingoSceneMetadata = Coolzon.Metadata.SolutionUtil.GetScene(Solution.RT_Solution, sceneName);

            if (bingoSceneMetadata != null)
            {
                CurrentDesignMetadata4Scene = Designtime.Metadata.MetadataUtil.GetScene(Solution, sceneName);

                UIContext.Current.CurrentScene = CurrentDesignMetadata4Scene;

                CurrentScene = bingoSceneMetadata;
                if (CurrentScene.IsShowInNavigation)
                {
                    if (Solution.RT_Solution.PartCacheMode == Coolzon.Metadata.PartCacheMode.NoCache)
                    {
                        scene = PartInstanceCache.GetSceneInstance("scene", bingoSceneMetadata, false);
                        scene.Init(_ctlMainFrame.PartContext);
                    }
                    else
                    {
                        scene = PartInstanceCache.GetSceneInstance("scene", bingoSceneMetadata, true);
                    }
                }
                else
                {
                    if (Solution.RT_Solution.PartCacheMode == Coolzon.Metadata.PartCacheMode.NoCache)
                    {
                        scene = PartInstanceCache.GetSceneInstance("scene", bingoSceneMetadata, false);
                        scene.Init(_ctlMainFrame.PartContext);
                    }
                    else
                    {
                        scene = PartInstanceCache.GetSceneInstance("scene", bingoSceneMetadata, true);
                    }
                }

                if (scene != null)
                {
                    _ctlMainFrame.ShowPart(scene, CPPEI.Coolzon.UI.SwitchAnimationFactory.Create("None"));
                }

                _ctlMainFrame.Active(bingoSceneMetadata, UIContext.Current.IsShowActiveXPart, null);

                SetScrollBar();

                _RaiseSceneOpenedEvent(scene as CPPEI.Coolzon.UI.ScenePart);

                if(UIContext.Current.CurrentEditedNode != null && !UIContext.Current.CurrentEditedNode.Isdirty)
                {
                    CaptureThumbnail();
                }
            }
        }

        /// <summary>
        /// 加载之后截取缩略图
        /// </summary>
        public void CaptureThumbnail()
        {
            Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Loaded, new Action(() =>
            {
                CaptureThumbnailImmediate();
            }));
        }
        /// <summary>
        /// 立即截取缩略图
        /// </summary>
        public void CaptureThumbnailImmediate()
        {
            ThumbnailHelper.SaveToImage(scene, (int)scene.XWidth, (int)scene.XHeight, UIContext.Current.CurrentScene.ID.ToString());
            if(UIContext.Current.CurrentEditedNode != null)
            {
                UIContext.Current.CurrentEditedNode.UpdateThumbnail();
            }
        }

        public string GetPartId()
        {
            this.CurrentDesignMetadata4Scene.IdSeed++;
            return string.Format("组件{0}", this.CurrentDesignMetadata4Scene.IdSeed);
        }

        /// <summary>
        /// 为part显示高亮边框
        /// </summary>
        /// <param name="part"></param>
        private void _ShowHLBorder4Part(Coolzon.UI.Part part)
        {
            if (part == null)
            {
                this._ctlPreviewRect.Visibility = System.Windows.Visibility.Hidden;
                return;
            }

            Point offsetOfPart = part.XGetAbsolutePos();
            Size size = new Size(part.XWidth, part.XHeight);

            this._ctlPreviewRect.Visibility = System.Windows.Visibility.Visible;
            Canvas.SetLeft(this._ctlPreviewRect, (offsetOfPart.X - _ctlScrollBarH.Value) * _ctlMainFrame.ZoomFactor);
            Canvas.SetTop(this._ctlPreviewRect, (offsetOfPart.Y - _ctlScrollBarV.Value) * _ctlMainFrame.ZoomFactor);
            this._ctlPreviewRect.Width = size.Width * _ctlMainFrame.ZoomFactor;
            this._ctlPreviewRect.Height = size.Height * _ctlMainFrame.ZoomFactor;
        }

        private void ShowGridAlignLineAndMoveDesignBorderTo(int resizeDirection, UICore.Part selectedPart)
        {
            UICore.IPartContainer partContainer = selectedPart.ParentPart as UICore.IPartContainer;

            Point leftTop = this._ctlDesignBorder.GetOffset();
            Size widthHeight = this._ctlDesignBorder.GetSize();
            Point rightBottom = new Point(leftTop.X + widthHeight.Width, leftTop.Y + widthHeight.Height);
            Point srcLeftTopPointInContainer = DesignUIUtil.PointInCanvasToPartContainer(leftTop, _ctlCanvas, partContainer);
            Point srcRightBottomPointInContainer = DesignUIUtil.PointInCanvasToPartContainer(rightBottom, _ctlCanvas, partContainer);

            double leftInContainer = srcLeftTopPointInContainer.X;
            double topInContainer = srcLeftTopPointInContainer.Y;
            double rightInContainer = srcRightBottomPointInContainer.X;
            double bottomInContainer = srcRightBottomPointInContainer.Y;
            double left = this._ctlDesignBorder.GetOffset().X;
            double top = this._ctlDesignBorder.GetOffset().Y;
            double width = this._ctlDesignBorder.GetSize().Width;
            double height = this._ctlDesignBorder.GetSize().Height;
            double right = left + width;
            double bottom = top + height;

            Tuple<Point?, Point?, byte> result;
            Point? leftOrRightInCanvas = null;
            Point? topOrBottomInCanvas = null;
            byte bDirection = 0;
            #region 定位自动对齐的坐标
            switch (resizeDirection)
            {
                case 0:
                    result = partContainer.GetAlignLinePoint(selectedPart, leftInContainer, topInContainer);
                    bDirection = result.Item3;
                    if (result.Item1 != null)
                    {
                        leftOrRightInCanvas = DesignUIUtil.PointInPartContainerToCanvas(result.Item1.Value, partContainer, _ctlCanvas);
                    }
                    if (result.Item2 != null)
                    {
                        topOrBottomInCanvas = DesignUIUtil.PointInPartContainerToCanvas(result.Item2.Value, partContainer, _ctlCanvas);
                    }
                    break;
                case 1:
                    result = partContainer.GetAlignLinePoint(selectedPart, -1, topInContainer);
                    bDirection = result.Item3;
                    if (result.Item2 != null)
                    {
                        topOrBottomInCanvas = DesignUIUtil.PointInPartContainerToCanvas(result.Item2.Value, partContainer, _ctlCanvas);
                    }
                    break;
                case 2:
                    result = partContainer.GetAlignLinePoint(selectedPart, -1, topInContainer, rightInContainer);
                    bDirection = result.Item3;
                    if (result.Item1 != null)
                    {
                        leftOrRightInCanvas = DesignUIUtil.PointInPartContainerToCanvas(result.Item1.Value, partContainer, _ctlCanvas);
                    }
                    if (result.Item2 != null)
                    {
                        topOrBottomInCanvas = DesignUIUtil.PointInPartContainerToCanvas(result.Item2.Value, partContainer, _ctlCanvas);
                    }
                    break;
                case 3:
                    result = partContainer.GetAlignLinePoint(selectedPart, leftInContainer);
                    bDirection = result.Item3;
                    if (result.Item1 != null)
                    {
                        leftOrRightInCanvas = DesignUIUtil.PointInPartContainerToCanvas(result.Item1.Value, partContainer, _ctlCanvas);
                    }
                    break;
                case 4:
                    result = partContainer.GetAlignLinePoint(selectedPart, leftInContainer, topInContainer, rightInContainer, bottomInContainer);
                    bDirection = result.Item3;
                    if (result.Item1 != null)
                    {
                        leftOrRightInCanvas = DesignUIUtil.PointInPartContainerToCanvas(result.Item1.Value, partContainer, _ctlCanvas);
                    }
                    if (result.Item2 != null)
                    {
                        topOrBottomInCanvas = DesignUIUtil.PointInPartContainerToCanvas(result.Item2.Value, partContainer, _ctlCanvas);
                    }
                    break;
                case 5:
                    result = partContainer.GetAlignLinePoint(selectedPart, -1, -1, rightInContainer);
                    bDirection = result.Item3;
                    if (result.Item1 != null)
                    {
                        leftOrRightInCanvas = DesignUIUtil.PointInPartContainerToCanvas(result.Item1.Value, partContainer, _ctlCanvas);
                    }
                    break;
                case 6:
                    result = partContainer.GetAlignLinePoint(selectedPart, leftInContainer, -1, -1, bottomInContainer);
                    bDirection = result.Item3;
                    if (result.Item1 != null)
                    {
                        leftOrRightInCanvas = DesignUIUtil.PointInPartContainerToCanvas(result.Item1.Value, partContainer, _ctlCanvas);
                    }
                    if (result.Item2 != null)
                    {
                        topOrBottomInCanvas = DesignUIUtil.PointInPartContainerToCanvas(result.Item2.Value, partContainer, _ctlCanvas);
                    }
                    break;
                case 7:
                    result = partContainer.GetAlignLinePoint(selectedPart, -1, -1, -1, bottomInContainer);
                    bDirection = result.Item3;
                    if (result.Item2 != null)
                    {
                        topOrBottomInCanvas = DesignUIUtil.PointInPartContainerToCanvas(result.Item2.Value, partContainer, _ctlCanvas);
                    }
                    break;
                case 8:
                    result = partContainer.GetAlignLinePoint(selectedPart, -1, -1, rightInContainer, bottomInContainer);
                    bDirection = result.Item3;
                    if (result.Item1 != null)
                    {
                        leftOrRightInCanvas = DesignUIUtil.PointInPartContainerToCanvas(result.Item1.Value, partContainer, _ctlCanvas);
                    }
                    if (result.Item2 != null)
                    {
                        topOrBottomInCanvas = DesignUIUtil.PointInPartContainerToCanvas(result.Item2.Value, partContainer, _ctlCanvas);
                    }
                    break;
                default:
                    result = new Tuple<Point?, Point?, byte>(null, null, 0);
                    break;
            }
            #endregion

            if(leftOrRightInCanvas != null)
            {
                _ctlAlignLineX.Visibility = Visibility.Visible;
                _ctlAlignLineX.X1 = leftOrRightInCanvas.Value.X;
                _ctlAlignLineX.Y1 = Math.Min(leftOrRightInCanvas.Value.Y, top);
                _ctlAlignLineX.X2 = leftOrRightInCanvas.Value.X;
                _ctlAlignLineX.Y2 = Math.Max(leftOrRightInCanvas.Value.Y, top + height);
                if (resizeDirection == 0 || resizeDirection == 3 || resizeDirection == 6)
                {
                    this._ctlDesignBorder.SetOffset(new Point(leftOrRightInCanvas.Value.X, top));
                    this._ctlDesignBorder.SetSize(new Size(right - leftOrRightInCanvas.Value.X, height));
                }
                else if(resizeDirection == 2 || resizeDirection == 5 || resizeDirection == 8)
                {
                    this._ctlDesignBorder.SetSize(new Size(leftOrRightInCanvas.Value.X - left, height));
                }
                else if(resizeDirection == 4)
                {
                    if ((bDirection & 1) == 0)
                    {
                        this._ctlDesignBorder.SetOffset(new Point(leftOrRightInCanvas.Value.X, top));
                    }
                    else
                    {
                        this._ctlDesignBorder.SetOffset(new Point(leftOrRightInCanvas.Value.X - width, top));
                    }
                }
                //如果已经自动对齐过，需要重新获取left和width属性
                left = this._ctlDesignBorder.GetOffset().X;
                width = this._ctlDesignBorder.GetSize().Width;
            }
            else
            {
                _ctlAlignLineX.Visibility = Visibility.Collapsed;
            }

            if (topOrBottomInCanvas != null)
            {
                _ctlAlignLineY.Visibility = Visibility.Visible;
                _ctlAlignLineY.X1 = Math.Min(topOrBottomInCanvas.Value.X, left);
                _ctlAlignLineY.Y1 = topOrBottomInCanvas.Value.Y;
                _ctlAlignLineY.X2 = Math.Max(topOrBottomInCanvas.Value.X, left + width);
                _ctlAlignLineY.Y2 = topOrBottomInCanvas.Value.Y;
                if (resizeDirection == 0 || resizeDirection == 1 || resizeDirection == 2)
                {
                    this._ctlDesignBorder.SetOffset(new Point(left, topOrBottomInCanvas.Value.Y));
                    this._ctlDesignBorder.SetSize(new Size(width, bottom - topOrBottomInCanvas.Value.Y));
                }
                else if (resizeDirection == 6 || resizeDirection == 7 || resizeDirection == 8)
                {
                    this._ctlDesignBorder.SetSize(new Size(width, topOrBottomInCanvas.Value.Y - top));
                }
                else if (resizeDirection == 4)
                {
                    if ((bDirection & 2) == 0)
                    {
                        this._ctlDesignBorder.SetOffset(new Point(left, topOrBottomInCanvas.Value.Y));
                    }
                    else
                    {
                        this._ctlDesignBorder.SetOffset(new Point(left, topOrBottomInCanvas.Value.Y - height));
                    }
                }
            }
            else
            {
                _ctlAlignLineY.Visibility = Visibility.Collapsed;
            }
        }
        /// <summary>
        /// 隐藏对齐指示线
        /// </summary>
        private void HideGridAlignLine()
        {
            _ctlAlignLineX.Visibility = Visibility.Collapsed;
            _ctlAlignLineY.Visibility = Visibility.Collapsed;
        }


    }
}
